package com.whfp.coky.ui.main.vm

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import com.whfp.coky.databinding.FragNonFixedBinding
import com.whfp.coky.other.Constants
import com.whfp.coky.ui.base.BaseFragment
import com.whfp.coky.ui.base.model.RequestBean
import com.whfp.coky.ui.base.vm.RecycleVM
import com.whfp.coky.ui.main.frag.TaskNonFixedFrag
import com.whfp.coky.ui.main.model.TaskCount02Bean
import com.whfp.coky.ui.main.model.TaskNonFixedBean

/**
 *
 *
 * created by coky on 2022/3/3
 */
class TaskNonFixedFragVM : RecycleVM<FragNonFixedBinding, TaskNonFixedBean>() {

    /**
     * recycle联动的网络请求
     */
    override suspend fun httpBlock(page: Int) = httpUtil.doTaskList02(mRequest.value as ReqTasks,page,Constants.PAGE_SIZE)

    var taskCountLD = MutableLiveData<TaskCount02Bean>()

    override fun httpDatas(page: Int) {
        super.httpDatas(page)
//        额外的网络请求在刷新的时候进行网络请求
        if (page == Constants.PAGE_INDEX){
            launch({httpUtil.doTaskCount02()},taskCountLD,false)
        }
    }

    /**
     * 监听数据变化，完成数据填充
     */
    override fun observe(fragment: BaseFragment<*, *>, owner: LifecycleOwner) {
        super.observe(fragment, owner)
        taskCountLD.observe(fragment, Observer {
            (fragment as TaskNonFixedFrag).initTaskCount(it)
        })
    }

    override fun getStatusLayout() = vBinding.statusHint

    /**
     * 装载查询数据，启动列表查询
     */
    override fun buildRequest() {
        mRequest.value = ReqTasks("")
    }

    /**
     * 修改列表查询参数
     */
    fun changeReq(taskName:String) {
        if (taskName.isEmpty()){
            buildRequest()
        }else{
            mRequest.value =  ReqTasks(taskName)
        }
    }

    /**
     * 任务列表请求bean
     */
    class ReqTasks(var taskName: String): RequestBean(){
        val equipment:String = "115"
        val system:String = "118"
        override fun toString(): String {
            return "ReqTasks(taskName='$taskName', equipment='$equipment', system='$system')"
        }
    }

}